package com.keai.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keai.constant.MessageConst;
import com.keai.entity.PageResult;
import com.keai.entity.QueryPageBean;
import com.keai.entity.Result;
import com.keai.pojo.Role;
import com.keai.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @program: SpringMVC_test
 * @author: Mr.Dong
 * @create: 2019-08-25 08:05
 */
@RestController
@RequestMapping("/role")
public class RoleController {


    @Reference
    RoleService roleService;

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult= roleService.findPage(queryPageBean);
        return pageResult;
    }

    //添加方式一：使用map接收数据，并反序列化
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestBody Map<String,Object> map){
        JSONArray jsonArray = (JSONArray) map.get("permissionIds");
        Integer[] permissionIds=jsonArray.toArray(new Integer[]{});


        JSONArray jsonArray2 = (JSONArray) map.get("menuIds");
        Integer[] menuIds=jsonArray2.toArray(new Integer[]{});


        JSONObject jsonObject= (JSONObject) map.get("role");
        Role role = jsonObject.toJavaObject(Role.class);

        try {
            roleService.add(permissionIds,menuIds,role);
            return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_ROLE_FAIL);
        }
    }
    @RequestMapping("/delById")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delById(Integer id){
        try {
            roleService.delById(id);
            return new Result(true, MessageConst.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.DELETE_ROLE_FAIL);
        }
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findById(Integer id){
        try {
            Role role=roleService.findById(id);
            return new Result(true, MessageConst.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_ROLE_FALL);
        }
    }

    @RequestMapping("/findPermissionIdsByRoleId")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            Integer [] permissionIds =  roleService.findPermissionIdsByRoleId(id);
            return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_PERMISSION_FALL);
        }
    }

    @RequestMapping("/findMenuIdsByRoleId")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            Integer [] menuIds =  roleService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConst.QUERY_MENU_SUCCESS,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_MENU_FALL);
        }
    }
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result edit(@RequestBody Map<String,Object> map){
        JSONArray jsonArray = (JSONArray) map.get("permissionIds");
        Integer[] permissionIds=jsonArray.toArray(new Integer[]{});

        JSONArray jsonArray2 = (JSONArray) map.get("menuIds");
        Integer[] menuIds=jsonArray2.toArray(new Integer[]{});

        JSONObject jsonObject= (JSONObject) map.get("role");
        Role role = jsonObject.toJavaObject(Role.class);

        try {
            roleService.edit(permissionIds,menuIds,role);
            return new Result(true, MessageConst.UPDATE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.UPDATE_ROLE_FALL);
        }
    }

    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findAll(){
        try {
         List<Role> roleList=roleService.findAll();
            return new Result(true, MessageConst.QUERY_ROLE_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_ROLE_FALL);
        }
    }
}
